{include file='common/head' /}
    <body>
        <style>
            img {
                padding:3px;
                border:1px solid #999;
                border-radius: 4px;
                max-width: 400px;
            }
        </style>
        <div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form">
                    <input type="hidden" name="id" value="{$data.id|default=''}">
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class="x-red">*</span>品牌名称
                        </label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" required="" autocomplete="off" class="layui-input" value="{$data.name|default=''}">
                        </div>
                        <div class="layui-form-mid layui-word-aux">
                            <span class="x-red">*</span>不允许为空
                        </div>
                    </div>
                    <!-- <div class="layui-form-item">
                        <label class="layui-form-label">
                            品牌图片
                        </label>
                        <div class="layui-input-block layui-upload">
                            <button type="button" class="layui-btn layui-btn-normal file" id="test1"><i class="layui-icon"></i>上传图片</button>
                            <input class="layui-upload-file" type="file" accept="undefined" name="file" id="test1s">
                            <span class="layui-inline layui-upload-choose">支持 png、jpg、jpeg 格式文件</span>
                            <img style="width:70px" id="img" src="{$data.cover|default=''}" alt="">
                        </div>
                    </div> -->

                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            活动封面
                        </label>
                        <div class="layui-input-block layui-upload">
                            <button type="button" class="layui-btn layui-btn-normal file" id="cover_img_btn"><i class="layui-icon"></i>上传图片</button>
                            <input class="layui-upload-file" type="file" accept="undefined" id="cover_img_file">
                            <span class="layui-inline layui-upload-choose">支持 png、jpg、jpeg 格式文件</span>
                        </div>
                        <div>
                            <img id="coverImg" src="{$data.cover|default=''}" alt="请选择图片" />
                            <input type="hidden" name="cover" value="{$data.cover|default=''}" />
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            品牌排序
                        </label>
                        <div class="layui-input-inline">
                            <input type="text" name="sort" required="" autocomplete="off" class="layui-input" value="{$data.sort|default='0'}">
                        </div>
                        <div class="layui-form-mid layui-word-aux">
                            <span class="x-red">*</span>值大的拍在前面，最高99999，不填默认0
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            是否推荐
                        </label>
                        <div class="layui-input-inline">
                            {if condition="empty($data['is_rec'])"}
                            <input type="radio" name="is_rec" value="1" title="是">
                            <input type="radio" name="is_rec" value="0" title="否" checked>
                            {else /}
                            <input type="radio" name="is_rec" value="1" title="是" {if condition="$data['is_rec'] eq 1"}checked{/if}>
                            <input type="radio" name="is_rec" value="0" title="否" {if condition="$data['is_rec'] eq 0"}checked{/if}>
                            {/if}
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label for="desc" class="layui-form-label">
                            品牌描述
                        </label>
                        <div class="layui-input-block">
                            <textarea placeholder="请输入内容" id="desc" name="desc" class="layui-textarea">{$data.desc|default=''}</textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"></label>
                        <button class="layui-btn" lay-filter="add" lay-submit="">确定保存</button></div>
                </form>
            </div>
        </div>
        <script>layui.use(['form', 'layer','jquery'],
            function() {
                $ = layui.jquery;
                var form = layui.form,
                layer = layui.layer;

                //监听提交
                form.on('submit(add)', function(data) {
                    var num = $('[lay-submit]').index($(this));
                    var formData = new FormData($('form')[num]);
                    //发异步，把数据提交给php
                    $.ajax({
                        url:"{:url('Product/brand_edit')}",
                        type: "post",
                        data:formData,
                        dataType:"json",
                        cache: false,                      // 不缓存
                        processData: false,                // jQuery不要去处理发送的数据
                        contentType: false,                // jQuery不要去设置Content-Type请求头
                        success:function (res) {
                            if(res.code == 200){
                                layer.alert(res.msg, {icon: 1},function () {
                                    //关闭当前frame
                                    xadmin.close();
                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                });
                            }else{
                                layer.alert(res.msg, {icon: 5});
                            }
                        },
                        error:function(e){
                            layer.alert("网络错误", {icon: 5});
                        },
                    });
                    return false;
                });

                //图片阅览
                $('#test1').click(function(){
                    $('#test1s').click();
                })
                $('#test1s').change(function(e){
                    var imgBox = e.target;
                    var img = getObjectURL(imgBox.files[0]);
                    $('#img').attr('src',img);
                });

                //建立一個可存取到該file的url
                function getObjectURL(file) {
                    var url;
                    if (window.createObjectURL != undefined) { // basic
                        url = window.createObjectURL(file);
                    } else if (window.URL != undefined) { // mozilla(firefox)
                        url = window.URL.createObjectURL(file);
                    } else if (window.webkitURL != undefined) { // webkit or chrome
                        url = window.webkitURL.createObjectURL(file);
                    }
                    return url;
                }

                // 绑定 活动封面
                $("#cover_img_btn").click(function(){
                    $("#cover_img_file").click();
                })

                // 操作成功后的回调 - 活动封面
                $('#cover_img_file').change(function(e){
                    var imgBox = e.target;
                    if(imgBox.files[0].size > 1024 * 1024 *2){
                        return layer.msg('失败，文件不得超过2mb',{icon: 5})
                    }
                    var img = getObjectURL(imgBox.files[0]);
                    $('#coverImg').attr('src',img);
                    var data = {image: imgBox.files[0]}
                    var formData = new FormData();
                    formData.append('image', imgBox.files[0]);
                    formData.append('prefix','product/brand');
                    $.ajax({
                        url:"{:url('admin/uploads/cosFiles')}",
                        type: 'post',
                        async: false,
                        data: formData,
                        dataType: 'json',
                        processData: false,// 告诉jQuery不要去处理发送的数据
                        contentType: false,// 告诉jQuery不要去设置Content-Type请求头
                        beforeSend: function (){ //过程...
                        },
                        success: function (res) {
                            if (res.code == 200) {
                                layer.msg('加载成功', {icon: 6})
                                //$('input[name=img_url]').val(HOST_URL +'/' +  res.data.fileName + '/' + (res.data.prefix ? '/' +  res.data.prefix + '/' : '') + res.data.url.replace("\\",'/'))
                                //$('input[name=img]').val( (res.data.prefix ? '/' +  res.data.prefix + '/' : '') + res.data.url.replace("\\",'/'))
                                $('input[name=cover]').val(res.data.url)
                            } else {
                                layer.msg(res.msg || '失败，麻烦重新尝试', {icon: 5})
                            }
                        },
                        error:function(){
                            layer.msg( '失败，重新尝试，多次失败后麻烦联系开发人员', {icon: 5})
                        }
                    })
                });

            });
        </script>
{include file='common/footer' /}